package com.selfmade.transferutil.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.selfmade.transferutil.R;
import com.selfmade.transferutil.app.MyApp;
import com.selfmade.transferutil.model.Book;

import java.util.ArrayList;
import java.util.List;

public class BookActivity extends AppCompatActivity {

    private ListView bookListView;
    private EditText bookName;
    private Button queryBook;
    private BookListAdapter bookListAdapter;
    List<Book> suitBookList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);

        //suitBookList = MyApp.bookList;

        bookName = (EditText) findViewById(R.id.bookName);
        queryBook = (Button) findViewById(R.id.query);
        bookListView = (ListView) findViewById(R.id.bookList);
        bookListAdapter = new BookListAdapter(BookActivity.this, 0, suitBookList);
        bookListView.setAdapter(bookListAdapter);
        queryBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bookName.getText().toString().equals("")) {
                    Toast.makeText(BookActivity.this, "请输入要查询的书名！", Toast.LENGTH_LONG).show();
                } else {
                    getSuitBookList(bookName.getText().toString());
                    bookListAdapter.notifyDataSetChanged();
                    System.out.println("点击了bu" + suitBookList.size());
                }
            }
        });
    }

    private void getSuitBookList(String name) {
        suitBookList.clear();
        int length = name.length();
        for (Book book : MyApp.bookList) {
            String b = book.getName();
            System.out.println("点击了for1: " + b);
            breakFor: for (int i = 0; i < length; i++) {
                for(int j = 0; j < b.length(); j++) {
                    if (name.charAt(i) == b.charAt(j)) {
                        suitBookList.add(book);
                        System.out.println("点击了for3" + b);
                        break breakFor;
                    }
                }
            }
        }
    }

}
